package com.robaone.gwt.hierarcialdb.client.ui.admin;

import java.util.HashMap;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.InlineLabel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.Widget;
import com.robaone.gwt.hierarcialdb.client.Hierarcialdbadmin;
import com.robaone.gwt.hierarcialdb.client.data.DataConstants;
import com.robaone.gwt.hierarcialdb.client.state.AdminState;

public class FileViewerUi extends Composite {

	private static FileViewerUiUiBinder uiBinder = GWT
			.create(FileViewerUiUiBinder.class);

	interface FileViewerUiUiBinder extends UiBinder<Widget, FileViewerUi> {
	}

	public FileViewerUi(HashMap<String, String> hashMap) {
		initWidget(uiBinder.createAndBindUi(this));
		this.m_data = hashMap;
		name.setText(hashMap.get("title"));
		content.setText(hashMap.get("value"));
		description.setHTML(hashMap.get("description"));
		this.setIcon(hashMap);
	}
	public void setIcon(HashMap<String, String> info){
		Resources res =  GWT.create(Resources.class);
		int ftype = Integer.parseInt(info.get("type"));
		if(ftype == DataConstants.STRING){
			icon.setResource(res.string_file());
		}else if(ftype == DataConstants.INT){
			icon.setResource(res.number_file());
		}else if(ftype == DataConstants.DOUBLE){
			icon.setResource(res.number_file());
		}else if(ftype == DataConstants.BOOLEAN){
			icon.setResource(res.bit_file());
		}else if(ftype == DataConstants.DATETIME){
			icon.setResource(res.date_file());
		}else if(ftype == DataConstants.TEXT){
			icon.setResource(res.text_file());
		}else if(ftype == DataConstants.BINARY){
			icon.setResource(res.binary_file());
			content.setVisible(false);
			binary_area.setVisible(true);
			file_link.setText(info.get("name"));
			try{
			file_link.setHref(Hierarcialdbadmin.getFileServletDownloadUrl(info.get("id")));
			}catch(Exception e){e.printStackTrace();}
		}else if(ftype == DataConstants.JSON){
			icon.setResource(res.json_file());
		}
		filename.setText(info.get("name"));
	}
	private HashMap<String,String> m_data;
	@UiField Image icon;
	@UiField InlineLabel name;
	@UiField TextArea content;
	@UiField HTML description;
	@UiField Label filename;
	@UiField Button editbutton;
	@UiField FlowPanel binary_area;
	@UiField Anchor file_link;
	
	@UiHandler("editbutton")
	public void handleEdit(ClickEvent event){
		if(this.getParent() instanceof FlowPanel){
			FlowPanel parent = (FlowPanel)this.getParent();
			parent.clear();
			FileEditorUi editor = new FileEditorUi(this.m_data);
			parent.add(editor);
		}
	}
	
	@UiHandler("closebutton")
	public void handleClose(ClickEvent event){
		AdminState.showFolder(this.m_data.get("parentid"));
	}
}
